<?php 
class PagerComponent extends Object
{    
    /**
     * The (calling) controller object.
     *
     * @access public
     * @var object
     */
     var $controller;

     /**
      * The pager object.
      *
      * @access public
      * @var object
      */
     var $pager;
     
     /**
      * Configuration parameters
      *
      * @access public
      * @var array
      */
     var $params;

    /**
     * Component pseudo controller
     *
     * @access public
     * @param object $controller Calling controller object
     * @return void
     */
    function startup(&$controller) {
        $this->controller =& $controller;
    }
    
    /**
     * Initializes the pager. Must be called before using the component.
     *
     * Takes user configuration and creates pager object ($this->pager)
     *
     * @access public
     * @param array $config Configuration options for Pager::factory() method
     * @see http://pear.php.net/manual/en/package.html.pager.factory.php
     * @return void
     */
    function init($config)
    {
		global $Dispatcher;
		$params = array(); //fix bug
        // Get the correct URL, even with admin routes
        $here = array();
        $here[0] = rtrim($this->controller->webroot, '/');
        if (defined('COLA_ADMIN') && !empty($Dispatcher->admin)) {
            $here[1] = COLA_ADMIN;
            $here[3] = substr($this->controller->params['action'], strlen(COLA_ADMIN) + 1);
        } else {
            $here[1] = false;
            $here[3] = $this->controller->params['action'];
        }		
        $here[2] = Inflector::underscore($this->controller->params['controller']);
        ksort($here);
        $url = implode('/', array_filter($here));
		if (strpos($url, '/') !== 0)
		{
			$url = '/' . $url;
		}
		
		if ( isset ($this->controller->params['form']) )
		{
			$params = array_merge($this->controller->params['pass'], $this->controller->params['form']);
		}
		
		if ( isset ($this->controller->params['url']) )
		{
			$params = array_merge($params, $this->controller->params['url']);
		}
		
        //$params = array_merge($this->controller->params['pass'], $this->controller->params['form'], $this->controller->params['url']);
        
        unset($params['page']);
        unset($params['url']);
		$q = http_build_query($params);
        if ($q == '')
        {
            $file_name = $url . '?' . 'page=%d';
        }
        else
        {
            $file_name = $url . '?' . $q. '&page=%d';
        }
    
        vendor('Pager/Pager');
        // Set up the default configuration vars
        $this->params = array(            
            'totalItems' => '',
            'perPage' => 10,
            'currentPage' => 1,
            'delta' => 10,             // for 'Jumping'-style a lower number is better
            'append' => false,
            'firstPagePre' => '',
            'firstPageText' => '',
            'firstPagePost' => '',
            'lastPagePre' => '',
            'lastPageText' => '',
            'lastPagePost' => '',
            'altFirst' => '第一页',
            'altPrev' => '上一页',
            'altNext' => '下一页',
            'altLast' => '最后一页',
            'prevImg' => '<',
            'nextImg' => '>',
        	'altPage' => '',
            'separator' => '',
            'curPageLinkClassName' => '"/><a class="pageNow',
            'spacesBeforeSeparator' => 0,
            'spacesAfterSeparator' => 0,
            'clearIfVoid' => true,
            'urlVar' => 'page',
            'sessionVar' => 'perpage',
            'useSessions' => false,
            'path' => '',            
            'httpMethod' => 'GET',
            'fileName' => $file_name,
            'mode'  => 'Sliding',    //try switching modes
            //'mode'  => 'Jumping'
        );
        /*
        $this->params = array(
            'mode' => 'Sliding',
            'perPage' => 10,
            'delta' => 5,
            'totalItems' => '',
            'httpMethod' => 'GET',
            'currentPage' => 1,
            'linkClass' => 'pager',
            'altFirst' => 'First page',
            'altPrev '=> 'Previous page',
            'altNext' => 'Next page',
            'altLast' => 'Last page',
            'separator' => '',
            'spacesBeforeSeparator' => 1,
            'spacesAfterSeparator' => 1,
            'useSessions' => false,
            'firstPagePre'	 => '',
            'firstPagePost' => '',
            'firstPageText' => '<<',
            'lastPagePre' => '',
            'lastPagePost' => '',
            'lastPageText' => '>>',
            'prevImg' => '<',
            'nextImg' => '>',
            'altPage' => 'Page',
            'clearIfVoid' => true,
            'append' => false,
            'path' => '',
            //'fileName' => $this->controller->base . DS . $url . DS . (($pass)?$pass.DS:'') . '?'.$q.'page=%d',
			'fileName' => $this->controller->webroot. encodeurl($this->controller->params['url']['url']) . '?'.$q.'page=%d',
            'urlVar' => '',
        );
        */        
        
        // Merge with user config
        $this->params = array_merge($this->params, $config);
 
        // sanitize requested page number
        if (!in_array($this->params['currentPage'], range(1, ceil($this->params['totalItems'] / $this->params['perPage'])))) {
            $this->params['currentPage'] = 1;
        }
        $this->pager =& Pager::factory($this->params);
        
        // Set the template vars
        $this->controller->set('pageLinks',   $this->pager->getLinks());
        $this->controller->set('currentPage', $this->params['currentPage']);
        $this->controller->set('totalItems', $this->params['totalItems']);
        $this->controller->set('perPage', $this->params['perPage']);
        $this->controller->set('isFirstPage', $this->pager->isFirstPage());
        $this->controller->set('isLastPage',  $this->pager->isLastPage());
    }
}
?>